# ============================================================
# Thomas König & Stefan Eschenwecker
# The European Court of Justice and legal European integration
# 
# This script produces Table A1 in the online appendix.
# ============================================================

# load packages
library(vtable)
library(tidyverse)

# load data
CourtData <- read.csv("Court/Data/PreparedCourtData.csv")


# create names for labels
VarLabs <- data.frame(
  var = c(
    "pos_cjeu_ln",
    "supra_signal", "supra_signal_int",
    "net_ms_pref", "sum_ms_me", "sum_ms_ps", "ms_signal_int",
    "share_judges",
    "all_not_applic", "ms_obs_ag",
    "about_derogation", "about_validity", 
    "about_direct_effect", "count_subject_keywords"
  ),
  labs = c(
    "Position ECJ",
    "EU Signal Direction", "EU Signal Strength",
    "MS Weighted Net Preferences",
    "Sum MS More Europe", "Sum MS Preserve Sovereignty",
    "MS Signal Strength",
    "Share Judges",
    "All Actors Not Applicable",
    "Home State AG Observation",
    "About Derogation", "About Validity",
    "About Direct Effect", "Number of Subject Keywords"
  )
)

# select relevant variables + recode data for table
DescData <- CourtData %>%
  select(
    pos_cjeu_ln,
    supra_signal, supra_signal_int, net_ms_pref,
    sum_ms_me, sum_ms_ps, ms_signal_int,
    share_judges, all_not_applic, ms_obs_ag,
    about_derogation, about_validity,
    about_direct_effect, count_subject_keywords
  ) %>%
  mutate(
    pos_cjeu_ln = factor(case_when(
      pos_cjeu_ln == "Ambi" ~ "Ambivalent",
      pos_cjeu_ln == "ME" ~ "More Europe",
      pos_cjeu_ln == "PS" ~ "Preserve Sovereignty"
    ), ordered = T, levels = c(
      "Preserve Sovereignty",
      "Ambivalent",
      "More Europe"
    )),
    supra_signal = factor(case_when(
      supra_signal == "Uninfo" ~ "Ambivalent",
      supra_signal == "Cont" ~ "Contradictory",
      supra_signal == "Weak ME" ~ "Single ME",
      supra_signal == "Weak PS" ~ "Single PS",
      supra_signal == "Strong ME" ~ "Both ME",
      supra_signal == "Strong PS" ~ "Both PS"
    ), ordered = F, levels = c(
      "Both PS",
      "Single PS",
      "Ambivalent",
      "Contradictory",
      "Single ME",
      "Both ME"
    )),
    supra_signal_int = factor(case_when(
      supra_signal_int == "No Intensity" ~ "No Strength",
      supra_signal_int == "Weak Intensity" ~ "Weak Strength",
      supra_signal_int == "Strong Intensity" ~ "Strong Strength"),
      ordered = T,
      levels = c(
        "No Strength",
        "Weak Strength",
        "Strong Strength"
      )
    ),
    all_not_applic = factor(ifelse(all_not_applic == 0, "No", "Yes")),
    ms_obs_ag = factor(ifelse(ms_obs_ag == 0, "No", "Yes")),
    about_derogation = factor(ifelse(about_derogation == "yes", "Yes", "No")),
    about_direct_effect = factor(ifelse(about_direct_effect == "yes", "Yes", "No")),
    about_validity = factor(ifelse(about_validity == "yes", "Yes", "No"))
  )

# generate Latex output
st(DescData,
  summ = c(
    "notNA(x)", "mean(x)", "sd(x)",
    "min(x)", "max(x)"
  ), out = "latex",
  digits = 1, fixed.digits = T, numformat = NA,
  summ.names = c(
    "N", "Mean", "SD",
    "Min", "Max"
  ), fit.page = NA,
  labels = VarLabs,
  title = "Summary statistics on Issues and Positions Data (including not applicable)."
)
